package springboot.config;

import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springboot.job.UserJob;

@Configuration
public class QuartzConfig {

    @Bean
    public JobDetail jobDetail(){
        return JobBuilder.newJob(UserJob.class).storeDurably().build();
    }

    @Bean
    public Trigger getTrigger(){
        //simple trigger
        SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInSeconds(1)//循环的事件
                .withRepeatCount(5);//循环的次数，可以永久的循环下去
        return TriggerBuilder.newTrigger()
                .withIdentity("trigger with 5 count","group for 5")
                .withSchedule(simpleScheduleBuilder)
                .forJob(jobDetail())
                .build();
    }

    @Bean
    public Trigger getTriggerForever(){
        return TriggerBuilder.newTrigger()
                .withIdentity("trigger forever","group forever")
                //CronTrigger 0表示每隔一分钟触发一次 0/5表示每隔五秒执行一次
                .withSchedule(CronScheduleBuilder.cronSchedule("0 * * * * ? *"))
                .forJob(jobDetail())
                .build();
    }
}
